package com.youbug.mall.product_pms.controller;


import com.youbug.common.vo.QueryVo;
import com.youbug.mall.product_pms.entity.PmsAttrGroup;
import com.youbug.mall.product_pms.entity.PmsProductAttrValue;
import com.youbug.mall.product_pms.entity.PmsSpuInfo;
import com.youbug.mall.product_pms.service.IAttrGroupService;
import com.youbug.mall.product_pms.service.IPmsProductAttrValueService;
import com.youbug.mall.product_pms.service.IPmsSpuInfoService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youbug.mall.product_pms.vo.SpuSaveVo;
import org.springframework.web.bind.annotation.*;
import com.youbug.common.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * spu信息 前端控制器
 * </p>
 *
 * @author youbug
 * @since 2024-04-21
 */
@RestController
@RequestMapping("/pmsSpuInfo")
public class PmsSpuInfoController {
    @Autowired
    private IPmsSpuInfoService pmsSpuInfoService;

    @Autowired
    private IAttrGroupService attrGroupService;

    @Autowired
    private IPmsProductAttrValueService productAttrValueService;

    @PostMapping("/getPmsSpuInfoPage")
    public R<IPage<PmsSpuInfo>> getPmsSpuInfoPage(@RequestBody QueryVo<PmsSpuInfo> queryVo){

        IPage<PmsSpuInfo> pmsSpuInfos = pmsSpuInfoService.getPmsSpuInfoPage(queryVo);

        return R.ok(pmsSpuInfos);
    }

    @GetMapping("/getPmsSpuInfoById/{id}")
    public R<PmsSpuInfo> getPmsSpuInfoById(@PathVariable Long id){

        PmsSpuInfo pmsSpuInfo = pmsSpuInfoService.getById(id);

        return R.ok(pmsSpuInfo);
    }

    @GetMapping("/getAttrGroupWithAttrByCatalogId/{catalogId}")
    public R<List<PmsAttrGroup>> getAttrGroupWithAttrByCatalogId(@PathVariable Long catalogId){

        List<PmsAttrGroup> attrGroups = attrGroupService.getAttrGroupWithAttrByCatalogId(catalogId);

        return R.ok(attrGroups);
    }

    @GetMapping("/getPmsSpuBaseAttrs/{id}")
    public R<List<PmsProductAttrValue>> getPmsSpuBaseAttrs(@PathVariable Long id){

        List<PmsProductAttrValue> values = productAttrValueService.getPmsSpuBaseAttrs(id);

        return R.ok(values);
    }

    @PostMapping("/updateSpuBaseAttrValues")
    public R<Boolean> updateSpuBaseAttrValues(@RequestBody List<PmsProductAttrValue> attrValues){

        boolean result = productAttrValueService.updateSpuBaseAttrValues(attrValues);

        return R.ok(result);
    }

    @GetMapping("/up/{spuId}")// 上架
    public R<Boolean> up(@PathVariable Long spuId){

        Boolean result = pmsSpuInfoService.up(spuId);

        return R.ok(result);
    }

    @GetMapping("/down/{spuId}")// 下架
    public R<Boolean> down(@PathVariable Long spuId){

        Boolean result = pmsSpuInfoService.down(spuId);

        return R.ok(result);
    }

    @PostMapping("/save")
    public R<Boolean> save(@RequestBody SpuSaveVo vo){

        boolean save = pmsSpuInfoService.saveSpuInfo(vo);

        return  R.ok(save);
    }

    @PostMapping("/update")
    public R<Boolean> update(@RequestBody PmsSpuInfo pmsSpuInfo){

        boolean update = pmsSpuInfoService.updateById(pmsSpuInfo);

        return  R.ok(update);
    }

    @GetMapping("/delete/{id}")
    public R<Boolean> delete(@PathVariable Long id){

        boolean remove = pmsSpuInfoService.removeSpu(id);

        return  R.ok(remove);
    }
}

